package pl.lodz.p.ftims.ProjektZespolowy2.utils;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import pl.lodz.p.ftims.ProjektZespolowy2.entities.Role;
import pl.lodz.p.ftims.ProjektZespolowy2.facades.RolesFacadeLocal;

@ManagedBean
@RequestScoped
public class RoleConverter implements Converter {

	@EJB
	private RolesFacadeLocal rolesFacade;
	
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		Integer id = Integer.parseInt(value);
		if ((rolesFacade == null)) {
			throw new ConverterException("Facade not found: " + value);
		}
		return rolesFacade.find(id);
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		if (value == null) {
			return null;
		}
		if ((value instanceof Role)) {
			Role role = (Role) value;
			return String.valueOf(role.getRoleId());
		}
		return String.valueOf(value);
	}

}
